/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;

import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.table.TableModel;

import rmiclient.Service;
import util.Institution;

/**
 * 
 * @author salim
 */
public class AppClient extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private void filltab() {
		employeeListTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][]
				// TODO
				{// Service.getServiceInstance().getInstitution(institutionsComboBox.getSelectedItem().toString()).getEmployeesList()
				{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }

				}, new String[] { "First name", "Last name",
						"National Id Number", "Phone number" }) {
			/**
				 * 
				 */
			private static final long serialVersionUID = 1L;
			@SuppressWarnings("rawtypes")
			Class[] types = new Class[] { java.lang.String.class,
					java.lang.String.class, java.lang.String.class,
					java.lang.String.class };
			boolean[] canEdit = new boolean[] { false, false, false, false };

			@Override
			public Class<?> getColumnClass(int columnIndex) {
				return types[columnIndex];
			}

			@Override
			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});

		Institution inst = null;

		try {
			inst = Service.getServiceInstance().getInstitution(
					institutionsComboBox.getSelectedItem().toString());
			List<String[]> list = inst.getEmployeesList();
			TableModel m = employeeListTable.getModel();

			int i = 0, j = 0;
			for (String[] strings : list) {
				for (j = 0; j < 4; j++) {
					m.setValueAt(strings[j], i, j);
				}
				i++;
			}

		} catch (Exception e) {
			JOptionPane
					.showMessageDialog(
							this,
							"Please check service configuration or the service backbone.",
							"Unable to invoke service",
							JOptionPane.WARNING_MESSAGE);
		}

	}

	private void initialfilltab() {
		employeeListTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][]
				// TODO
				{// Service.getServiceInstance().getInstitution(institutionsComboBox.getSelectedItem().toString()).getEmployeesList()
				{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null }

				}, new String[] { "First name", "Last name",
						"National Id Number", "Phone number" }) {
			/**
				 * 
				 */
			private static final long serialVersionUID = 1L;
			@SuppressWarnings("rawtypes")
			Class[] types = new Class[] { java.lang.String.class,
					java.lang.String.class, java.lang.String.class,
					java.lang.String.class };
			boolean[] canEdit = new boolean[] { false, false, false, false };

			@Override
			public Class<?> getColumnClass(int columnIndex) {
				return types[columnIndex];
			}

			@Override
			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});

	}

	/**
	 * Creates new form MainForm
	 */
	public AppClient() {
		initComponents();

	}

	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jToolBar1 = new javax.swing.JToolBar();
		institutionsComboBox = new javax.swing.JComboBox<String>();
		jSeparator1 = new javax.swing.JToolBar.Separator();
		addButton = new javax.swing.JButton();
		updateButton = new javax.swing.JButton();
		deleteButton = new javax.swing.JButton();
		searchButton = new javax.swing.JButton();
		jScrollPane2 = new javax.swing.JScrollPane();
		employeeListTable = new javax.swing.JTable();
		jToolBar2 = new javax.swing.JToolBar();
		serverPropertiesButton = new javax.swing.JButton();
		refreshButton = new javax.swing.JButton();
		jMenuBar2 = new javax.swing.JMenuBar();
		jMenu3 = new javax.swing.JMenu();
		exitMenuItem = new javax.swing.JMenuItem();
		jMenu4 = new javax.swing.JMenu();
		serverPropertiesMenuItem = new javax.swing.JMenuItem();
		refreshMenuItem = new javax.swing.JMenuItem();
		jMenu1 = new javax.swing.JMenu();
		addMenuItem = new javax.swing.JMenuItem();
		updateMenuItem = new javax.swing.JMenuItem();
		deleteMenuItem = new javax.swing.JMenuItem();
		searchMenuItem = new javax.swing.JMenuItem();

		institutionsComboBox
				.setModel(new javax.swing.DefaultComboBoxModel<String>(
						new String[] { "FST", "ENIT", "ISI", "INSAT", "ENSIT",
								"ENSI" }));
		institutionsComboBox.setPreferredSize(new java.awt.Dimension(100, 30));
		institutionsComboBox
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						institutionsComboBoxActionPerformed(evt);
					}
				});

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jToolBar1.setOrientation(javax.swing.SwingConstants.VERTICAL);
		jToolBar1.setRollover(true);

		jToolBar1.add(institutionsComboBox);
		jToolBar1.add(jSeparator1);

		addButton.setText("add");
		addButton.setFocusable(false);
		addButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		addButton.setPreferredSize(new java.awt.Dimension(100, 30));
		addButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		addButton.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				addButtonActionPerformed(evt);
			}
		});
		jToolBar1.add(addButton);

		updateButton.setText("update");
		updateButton.setFocusable(false);
		updateButton
				.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		updateButton.setPreferredSize(new java.awt.Dimension(100, 30));
		updateButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		updateButton.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				updateButtonActionPerformed(evt);
			}
		});
		jToolBar1.add(updateButton);

		deleteButton.setText("delete");
		deleteButton.setFocusable(false);
		deleteButton
				.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		deleteButton.setPreferredSize(new java.awt.Dimension(100, 30));
		deleteButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		deleteButton.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				deleteButtonActionPerformed(evt);
			}
		});
		jToolBar1.add(deleteButton);

		searchButton.setText("search");
		searchButton.setFocusable(false);
		searchButton
				.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		searchButton.setPreferredSize(new java.awt.Dimension(100, 30));
		searchButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		searchButton.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				searchButtonActionPerformed(evt);
			}
		});
		jToolBar1.add(searchButton);

		getContentPane().add(jToolBar1, java.awt.BorderLayout.WEST);

		initialfilltab();

		employeeListTable.setColumnSelectionAllowed(true);
		employeeListTable.setDropMode(javax.swing.DropMode.ON);
		employeeListTable.getTableHeader().setReorderingAllowed(false);
		jScrollPane2.setViewportView(employeeListTable);
		employeeListTable
				.getColumnModel()
				.getSelectionModel()
				.setSelectionMode(
						javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		if (employeeListTable.getColumnModel().getColumnCount() > 0) {
			employeeListTable.getColumnModel().getColumn(0).setResizable(false);
			employeeListTable.getColumnModel().getColumn(1).setResizable(false);
			employeeListTable.getColumnModel().getColumn(2).setResizable(false);
			employeeListTable.getColumnModel().getColumn(3).setResizable(false);
		}

		getContentPane().add(jScrollPane2, java.awt.BorderLayout.CENTER);

		jToolBar2.setRollover(true);

		serverPropertiesButton.setText("Server");
		serverPropertiesButton.setFocusable(false);
		serverPropertiesButton
				.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		serverPropertiesButton
				.setPreferredSize(new java.awt.Dimension(100, 30));
		serverPropertiesButton
				.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		serverPropertiesButton
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						serverPropertiesButtonActionPerformed(evt);
					}
				});
		jToolBar2.add(serverPropertiesButton);

		refreshButton.setText("Refresh");
		refreshButton.setFocusable(false);
		refreshButton
				.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		refreshButton.setPreferredSize(new java.awt.Dimension(100, 30));
		refreshButton
				.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		refreshButton.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				refreshButtonActionPerformed(evt);
			}
		});
		jToolBar2.add(refreshButton);

		getContentPane().add(jToolBar2, java.awt.BorderLayout.PAGE_START);

		jMenu3.setText("File");

		exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_Q,
				java.awt.event.InputEvent.CTRL_MASK));
		exitMenuItem.setText("Exit");
		exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				exitMenuItemActionPerformed(evt);
			}
		});
		jMenu3.add(exitMenuItem);

		jMenuBar2.add(jMenu3);

		jMenu4.setText("Edit");

		serverPropertiesMenuItem.setAccelerator(javax.swing.KeyStroke
				.getKeyStroke(java.awt.event.KeyEvent.VK_P,
						java.awt.event.InputEvent.CTRL_MASK));
		serverPropertiesMenuItem.setText("Server properties");
		serverPropertiesMenuItem
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						serverPropertiesMenuItemActionPerformed(evt);
					}
				});
		jMenu4.add(serverPropertiesMenuItem);

		refreshMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_F5, 0));
		refreshMenuItem.setText("Refresh");
		refreshMenuItem.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				refreshMenuItemActionPerformed(evt);
			}
		});
		jMenu4.add(refreshMenuItem);

		jMenuBar2.add(jMenu4);

		jMenu1.setText("Manage");

		addMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_B,
				java.awt.event.InputEvent.CTRL_MASK));
		addMenuItem.setText("Add");
		addMenuItem.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				addMenuItemActionPerformed(evt);
			}
		});
		jMenu1.add(addMenuItem);

		updateMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_U,
				java.awt.event.InputEvent.CTRL_MASK));
		updateMenuItem.setText("Update");
		updateMenuItem.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				updateMenuItemActionPerformed(evt);
			}
		});
		jMenu1.add(updateMenuItem);

		deleteMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_D,
				java.awt.event.InputEvent.CTRL_MASK));
		deleteMenuItem.setText("Delete");
		deleteMenuItem.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				deleteMenuItemActionPerformed(evt);
			}
		});
		jMenu1.add(deleteMenuItem);

		searchMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_S,
				java.awt.event.InputEvent.CTRL_MASK));
		searchMenuItem.setText("Search");
		searchMenuItem.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				searchMenuItemActionPerformed(evt);
			}
		});
		jMenu1.add(searchMenuItem);

		jMenuBar2.add(jMenu1);

		setJMenuBar(jMenuBar2);

		pack();
	}// </editor-fold>

	private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
		DialogFactory.getInstance().createDialog(
				DialogType.SEARCH_EMPLOYEE_DIALOG, this,
				rootPaneCheckingEnabled,
				institutionsComboBox.getSelectedItem().toString());
	}

	private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
		DialogFactory.getInstance().createDialog(
				DialogType.ADD_EMPLOYEE_DIALOG, this, rootPaneCheckingEnabled,
				institutionsComboBox.getSelectedItem().toString());
	}

	private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {
		DialogFactory.getInstance().createDialog(
				DialogType.UPDATE_EMPLOYEE_DIALOG, this,
				rootPaneCheckingEnabled,
				institutionsComboBox.getSelectedItem().toString());
	}

	private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
		DialogFactory.getInstance().createDialog(
				DialogType.DELETE_EMPLOYEE_DIALOG, this,
				rootPaneCheckingEnabled,
				institutionsComboBox.getSelectedItem().toString());
	}

	private void serverPropertiesButtonActionPerformed(
			java.awt.event.ActionEvent evt) {
		DialogFactory.getInstance().createDialog(
				DialogType.SERVER_PROPERTIES_DIALOG, this,
				rootPaneCheckingEnabled, null);
	}

	private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		filltab();
	}

	private void institutionsComboBoxActionPerformed(
			java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		filltab();
	}

	private void serverPropertiesMenuItemActionPerformed(
			java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		this.serverPropertiesButtonActionPerformed(evt);
	}

	private void refreshMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		this.refreshButtonActionPerformed(evt);
	}

	private void addMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		this.addButtonActionPerformed(evt);
	}

	private void updateMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		this.updateButtonActionPerformed(evt);
	}

	private void deleteMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		this.deleteButtonActionPerformed(evt);
	}

	private void searchMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		this.searchButtonActionPerformed(evt);
	}

	private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		System.exit(NORMAL);
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException
				| javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(AppClient.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				new AppClient().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify
	private javax.swing.JButton addButton;
	private javax.swing.JMenuItem addMenuItem;
	private javax.swing.JButton deleteButton;
	private javax.swing.JMenuItem deleteMenuItem;
	private javax.swing.JTable employeeListTable;
	private javax.swing.JMenuItem exitMenuItem;
	private javax.swing.JComboBox<String> institutionsComboBox;
	private javax.swing.JMenu jMenu1;
	private javax.swing.JMenu jMenu3;
	private javax.swing.JMenu jMenu4;
	private javax.swing.JMenuBar jMenuBar2;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JToolBar.Separator jSeparator1;
	private javax.swing.JToolBar jToolBar1;
	private javax.swing.JToolBar jToolBar2;
	private javax.swing.JButton refreshButton;
	private javax.swing.JMenuItem refreshMenuItem;
	private javax.swing.JButton searchButton;
	private javax.swing.JMenuItem searchMenuItem;
	private javax.swing.JButton serverPropertiesButton;
	private javax.swing.JMenuItem serverPropertiesMenuItem;
	private javax.swing.JButton updateButton;
	private javax.swing.JMenuItem updateMenuItem;
	// End of variables declaration
}
